package com.deyuanyun.pic.ctrl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.deyuanyun.pic.business.controller.dto.PipeLineNewTaskVO;
import com.deyuanyun.pic.business.domain.MergeTaskBasis;
import com.deyuanyun.pic.business.service.MergeTaskService;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.ctrl.controller.dto.ExinspTaskExpandDTO;
import com.deyuanyun.pic.ctrl.controller.vo.ExinspTaskVO;
import com.deyuanyun.pic.ctrl.domain.ExinspTask;
import com.deyuanyun.pic.ctrl.domain.ExinspTaskExample;
import com.deyuanyun.pic.ctrl.enumeration.ExinspDataStatedEnum;
import com.deyuanyun.pic.ctrl.mapper.ExinspTaskMapper;
import com.deyuanyun.pic.ctrl.mapper.ExternalInspProjectMapper;
import com.deyuanyun.pic.ctrl.service.ExternalInspProjectService;
import com.deyuanyun.pic.settings.controller.pipe.vo.PipeLocationDTO;
import com.deyuanyun.pic.settings.domain.util.DomainInitUtil;
import com.deyuanyun.pic.settings.service.pipe.PipeLocationService;

/**
 *  外检项目service实现类
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-09-22
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
@Service
public class ExternalInspProjectServiceImpl implements ExternalInspProjectService {

    @Autowired ExternalInspProjectMapper externalInspProjectMapper;

    @Autowired ExinspTaskMapper exinspTaskMapper;

    @Autowired PipeLocationService pipeLocationService;
    @Autowired MergeTaskService mergeTaskService;
    

    @Override
    public List<ExinspTaskExpandDTO> queryExinspTasks(ExinspTaskVO exinspTaskVO) {
        return externalInspProjectMapper.queryExinspTasks(exinspTaskVO);
    }

    @Override
    public int updateDeleted(@Param("ids") String... ids) {
        return externalInspProjectMapper.updateDeleted(ids);
    }

    @Override
    public ExinspTaskExpandDTO getInfoById(String id) {
        ExinspTaskExpandDTO dto = new ExinspTaskExpandDTO();
        ExinspTask exinspTask = exinspTaskMapper.selectByPrimaryKey(id);
        if(exinspTask != null) {
            BeanUtils.copyProperties(exinspTask, dto);
            String fromId = dto.getFromPointId();
            String toId = dto.getToPointId();
            String pipeLineId = dto.getPipeLineId();
            if(StringUtils.isNotEmpty(fromId) && StringUtils.isNotEmpty(pipeLineId)) {
                PipeLocationDTO pipeLocationDTO = pipeLocationService.queryPipeLocationById(pipeLineId, fromId);
                if(pipeLocationDTO != null) {
                    dto.setFromName(pipeLocationDTO.getName());
                }
            }
            if(StringUtils.isNotEmpty(toId) && StringUtils.isNotEmpty(pipeLineId)) {
                PipeLocationDTO pipeLocationDTO = pipeLocationService.queryPipeLocationById(pipeLineId, toId);
                if(pipeLocationDTO != null) {
                    dto.setToName(pipeLocationDTO.getName());
                }
            }
        }
        return dto;
    }

    @Override
    public void createOrUpdate(ExinspTaskExpandDTO exinspTaskExpandDTO) {
        String id = exinspTaskExpandDTO.getId();
        ExinspTask exinspTask = new ExinspTask();
        BeanUtils.copyProperties(exinspTaskExpandDTO, exinspTask);
        if(!exinspTaskExpandDTO.isTestMethod3()) {
            exinspTask.setMethodOther("");
        }
        String name = exinspTask.getName();
        ExinspTaskVO exinspTaskVO = new ExinspTaskVO();
        if(StringUtils.isNotEmpty(id)) {
            ExinspTask task = exinspTaskMapper.selectByPrimaryKey(id);
            if(task != null) {
                ExinspDataStatedEnum statedEnum = ExinspDataStatedEnum.getInstance(task.getExinspDataStated());
                Asserts.assertTrue(ExinspDataStatedEnum.NOT_IMPORT.equals(statedEnum), "编辑失败。已导入外检数据的项目不可再编辑！");
            }
            exinspTaskVO.setExtName(task.getName()).setName(name);
            List<ExinspTaskExpandDTO> exinspTaskExpandDTOs = externalInspProjectMapper.queryExinspTasks(exinspTaskVO);
            Asserts.assertTrue(exinspTaskExpandDTOs == null || exinspTaskExpandDTOs.isEmpty(), "外检项目名称已存在，请修改");
            exinspTask.setExinspDataStated(null);
            DomainInitUtil.initUpdateDomain(exinspTask);
            exinspTaskMapper.updateByPrimaryKeySelective(exinspTask);
        } else {
            exinspTaskVO.setExtName("").setName(name);
            List<ExinspTaskExpandDTO> exinspTaskExpandDTOs = externalInspProjectMapper.queryExinspTasks(exinspTaskVO);
            Asserts.assertTrue(exinspTaskExpandDTOs == null || exinspTaskExpandDTOs.isEmpty(), "外检项目名称已存在，请修改");

            exinspTask.setIsNewest((short) 0);
            exinspTask.setExinspDataStated("NOT_IMPORT");
            String fromPointId = exinspTask.getFromPointId();
            String toPointId = exinspTask.getToPointId();
            String pipeLineId = exinspTask.getPipeLineId();
            if(StringUtils.isNotEmpty(fromPointId) && StringUtils.isNotEmpty(pipeLineId)) {
                PipeLocationDTO locationDTO =  pipeLocationService.queryPipeLocationById(pipeLineId, fromPointId);
                if(locationDTO != null) {
                    exinspTask.setFromPointType(locationDTO.getType());
                }
            }
            if(StringUtils.isNotEmpty(toPointId) && StringUtils.isNotEmpty(pipeLineId)) {
                PipeLocationDTO locationDTO =  pipeLocationService.queryPipeLocationById(pipeLineId, toPointId);
                if(locationDTO != null) {
                    exinspTask.setToPointType(locationDTO.getType());
                }
            }

            DomainInitUtil.initCreateDomain(exinspTask);
            exinspTaskMapper.insertSelective(exinspTask);
        }
    }

    @Override
    public List<PipeLineNewTaskVO> queryNewExternalTaskMerge(String pipeLineId) {
        
        return queryNewExternalTaskMerge(pipeLineId,null,null);
    }


    @Override
    public List<PipeLineNewTaskVO> queryNewExternalTaskMerge(String pipeLineId,Date beginDate, Date endDate) {

        ExinspTaskExample exinspTaskExample = new ExinspTaskExample();
        ExinspTaskExample.Criteria c = exinspTaskExample.createCriteria();
        c.andPipeLineIdEqualTo(pipeLineId).andDeletedEqualTo((short)0);
        if(beginDate != null){
            c.andTestingDateFromGreaterThanOrEqualTo(beginDate);
        }
        if(endDate != null){
            c.andTestingDateToLessThanOrEqualTo(endDate);
        }
        exinspTaskExample.setOrderByClause("TESTING_DATE_TO desc");
        List<ExinspTask> exinspTaskList = exinspTaskMapper.selectByExample(exinspTaskExample);
        
        List<MergeTaskBasis> list = new ArrayList<MergeTaskBasis>(exinspTaskList.size());
        for (int i = 0; i < exinspTaskList.size(); i++) {
            ExinspTask exinspTask = exinspTaskList.get(i);
            MergeTaskBasis mergeTaskBasis = new MergeTaskBasis();
            mergeTaskBasis.setId(exinspTask.getId());
            mergeTaskBasis.setFromPointMileage(exinspTask.getFromPointMileage());
            mergeTaskBasis.setToPointMileage(exinspTask.getToPointMileage());
            list.add(mergeTaskBasis);
        }
        List<PipeLineNewTaskVO> newTaskList = mergeTaskService.mergeTask(list);
        return newTaskList;
    }

    
    
    @Override
    public List<PipeLineNewTaskVO> queryNagmExternalTaskMerge(String pipeLineId, Date testDate, Boolean isAgo) {
        ExinspTaskExample exinspTaskExample = new ExinspTaskExample();
        ExinspTaskExample.Criteria c = exinspTaskExample.createCriteria();
        c.andPipeLineIdEqualTo(pipeLineId).andDeletedEqualTo((short)0);
        if(isAgo != null && isAgo){
            c.andTestingDateFromLessThanOrEqualTo(testDate);
            exinspTaskExample.setOrderByClause("TESTING_DATE_TO desc");
        }else{
            c.andTestingDateFromGreaterThanOrEqualTo(testDate);
            exinspTaskExample.setOrderByClause("TESTING_DATE_TO ");
        }
        
        List<ExinspTask> exinspTaskList = exinspTaskMapper.selectByExample(exinspTaskExample);
        List<MergeTaskBasis> list = new ArrayList<MergeTaskBasis>(exinspTaskList.size());
        for (int i = 0; i < exinspTaskList.size(); i++) {
            ExinspTask exinspTask = exinspTaskList.get(i);
            MergeTaskBasis mergeTaskBasis = new MergeTaskBasis();
            mergeTaskBasis.setId(exinspTask.getId());
            mergeTaskBasis.setFromPointMileage(exinspTask.getFromPointMileage());
            mergeTaskBasis.setToPointMileage(exinspTask.getToPointMileage());
            list.add(mergeTaskBasis);
        }
        
        List<PipeLineNewTaskVO> newTaskList = mergeTaskService.mergeTask(list);
        
        return newTaskList;
    }
    
    @Override
    public List<ExinspTask> queryExinspTask(String pipeLine, Long start,Long end) {
        
        ExinspTaskExample exinspTaskExample = new ExinspTaskExample();
        ExinspTaskExample.Criteria c = exinspTaskExample.createCriteria();
        c.andPipeLineIdEqualTo(pipeLine);
        if(start != null){
            c.andFromPointMileageLessThanOrEqualTo(start);
        }
        if(end != null){
            c.andToPointMileageGreaterThanOrEqualTo(end);
        }
        c.andDeletedEqualTo((short)0);
        exinspTaskExample.setOrderByClause("TESTING_DATE_TO desc");
        List<ExinspTask> exinspTaskList = exinspTaskMapper.selectByExample(exinspTaskExample);
        return exinspTaskList;
    }

    
}
